<?php
class SiteAction extends BaseAction {
	function _initialize() {
		parent::_initialize ();
		$this->dao = M ( 'User' );
	}
	
	/**
	 * 云购商列表
	 */
	function biz_list() {
		// $condition = parent::create_where ();
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		if ($_REQUEST ['sfield'] == 'id')
			$key = "u.id={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "u.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select u.id,u.username,u.email,u.mobile,u.status,u.last_logintime
				from `user` as u left join role_user as ru on u.id=ru.user_id where ru.role_id=15 and {$key} ORDER BY u.{$order}";
		$sql_c = "select count(*) num 
				from `user` as u left join role_user as ru on u.id=ru.user_id where ru.role_id=15 and {$key}";
		$sql_f = 'num';
		
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		foreach ( $list as &$value ) {
			$shops = M ( "user_ext" )->where ( array (
					'element' => 'cloudshop',
					'value' => $value ['id'] 
			) )->count ();
			$value ['shops'] = $shops;
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 云购店列表
	 */
	function site_list() {
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		if ($_REQUEST ['sfield'] == 'id')
			$key = "u.id={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "u.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select u.id,u.username,u.mobile,u.last_logintime,s.name,s.domain,s.status
				from user as u right join site as s on s.user_id=u.id where 1 and {$key} ORDER BY u.{$order}";
		$sql_c = "select count(*) num
				from user as u right join site as s on s.user_id=u.id where 1 and {$key}";
		$sql_f = 'num';
		
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		$this->assign ( 'list', $list ); // dump($list);
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 云购店/站点基本信息
	 */
	function biz_info() {
	}
	
	/**
	 * 云购店/站点所属用户基本信息
	 */
	function user_info() {
	}
	
	/**
	 * 布局显示
	 */
	function layout_list() {
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		if ($_REQUEST ['keywords'] == '显示')
			$_REQUEST ['keywords'] = 1;
		elseif ($_REQUEST ['keywords'] == '隐藏')
			$_REQUEST ['keywords'] = 2;
		
		$main_table = 'l';
		if (in_array ( $_REQUEST ['sfield'], array (
				'username' 
		) ))
			$main_table = 'u';
		if (in_array ( $_REQUEST ['sfield'], array (
				'id',
				'status',
				'type' 
		) ))
			$key = "{$main_table}.{$_REQUEST['sfield']}={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "{$main_table}.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select u.username,s.name as sname,t.name,l.layout,l.id,l.type,l.sequence,l.status
			from layout as l
			left join `user` as u on u.id=l.user_id
			left join site as s on s.user_id=l.user_id
			left join tags as t on t.id=l.cell_id where {$key} ORDER BY l.{$order}";
		$sql_c = "select count(*) num 
			from layout as l
			left join `user` as u on u.id=l.user_id
			left join site as s on s.user_id=l.user_id
			left join tags as t on t.id=l.cell_id where {$key}";
		$sql_f = 'num';
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		$this->assign ( 'list', $list ); // dump($list);
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 布局修改
	 */
	function layout_edit() {
	}
	
	/**
	 * 标签修改
	 */
	function tag_list() {
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		$main_table = 't';
		if (in_array ( $_REQUEST ['sfield'], array (
				'username' 
		) ))
			$main_table = 'u';
		if (in_array ( $_REQUEST ['sfield'], array (
				'id',
				'status',
				'type' 
		) ))
			$key = "{$main_table}.{$_REQUEST['sfield']}={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "{$main_table}.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select t.id,t.user_id,t.type,t.sequence,t.name,u.username,s.name as sname
			from tags as t
			left join `user` as u on u.id=t.user_id
			left join site as s on s.user_id=t.user_id where 1 and {$key} ORDER BY t.{$order}";
		$sql_c = "select count(*) num
			from tags as t
			left join `user` as u on u.id=t.user_id
			left join site as s on s.user_id=t.user_id where 1 and {$key}";
		$sql_f = 'num';
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		foreach ( $list as &$value ) {
			$re = M ()->query ( "select group_concat(layout) as l from layout where user_id={$value['user_id']} and cell_id={$value['id']}" );
			$layoutS = $re [0] ['l'];
			$layoutA = explode ( ',', $layoutS );
			$layoutC = array (
					'index-menu_left' => '下拉导航',
					'index-menu' => '下拉导航',
					'index-left' => '下拉导航',
					'index-rihgt' => '下拉导航',
					'index-banner2' => '下拉导航' 
			);
			foreach ( $layoutA as $key => &$v ) {
				$v = $layoutC [$v];
			}
			$layoutS = implode ( ',', $layoutA );
			$value ['layout'] = $layoutS;
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 标签修改
	 */
	function tag_edit() {
	}
	
	/**
	 * 产品列表
	 */
	function product_list() {
		$key = $key_u = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		$main_table = 'p';
		if (in_array ( $_REQUEST ['sfield'], array (
				'username' 
		) ))
			$main_table = 'p';
		if ($_REQUEST ['keywords'] && in_array ( $_REQUEST ['sfield'], array (
				'id',
				'status',
				'type' 
		) ))
			$key = "{$main_table}.{$_REQUEST['sfield']}={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['keywords'] && $_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "{$main_table}.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		if ($_REQUEST ['keywords_u'] && in_array ( $_REQUEST ['sfield_u'], array (
				'id' 
		) ))
			$key_u = " u.{$_REQUEST['sfield_u']}={$_REQUEST['keywords_u']}";
		elseif ($_REQUEST ['keywords_u'] && $_REQUEST ['sfield_u'] && $_REQUEST ['sfield_u'] != 'all')
			$key_u = "u.{$_REQUEST['sfield_u']} like '%{$_REQUEST['keywords_u']}%'";
		
		$sql_s = "select s.name as sname,u.username,po.id as poid,p.id,po.status,p.name,p.code,p.create_date
			from product_o2o as po 
			left join product as p on p.id=po.product_id
			left join `user` as u on u.id=po.user_id
			left join site as s on s.user_id=po.user_id where 1 and {$key} and {$key_u}";
		$sql_c = "select count(*) num
			from product_o2o as po 
			left join product as p on p.id=po.product_id
			left join `user` as u on u.id=po.user_id
			left join site as s on s.user_id=po.user_id where 1 and {$key} and {$key_u}";
		$sql_f = 'num';
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		$this->assign ( 'list', $list ); // dump($list);die();
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->assign ( 'sfield_u', $_REQUEST ['sfield_u'] );
		$this->assign ( 'keywords_u', $_REQUEST ['keywords_u'] );
		$this->display ();
	}
	
	/**
	 * 广告列表
	 */
	function ad_list() {
		// $condition = parent::create_where ();
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		$main_table = 'a';
		if (in_array ( $_REQUEST ['sfield'], array (
				'username' 
		) ))
			$main_table = 'u';
		if ($_REQUEST ['sfield'] == 'id')
			$key = "{$main_table}.id={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "{$main_table}.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select a.id,a.title,a.url,a.type,a.starttime,a.endtime,a.status,s.name,s.user_id
			from ads as a
			left join site as s on s.user_id=a.user_id where {$key} ORDER BY a.{$order}";
		$sql_c = "select count(*) num 
				from ads as a left join site as s on s.user_id=a.user_id where {$key}";
		$sql_f = 'num';
		
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		foreach ( $list as &$value ) {
			$shops = M ()->query ( "select group_concat(name SEPARATOR '') address from `area` where id in({$value['province']},{$value['city']},{$value['district']})" );
			$value ['address'] = $shops [0] ['address'];
		}
		$this->assign ( 'list', $list ); // dump($list);
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 文章列表
	 */
	function article_list() {
		// $condition = parent::create_where ();
		$key = '1';
		$order = $this->orderField . ' ' . $this->orderDirection;
		$main_table = 'a';
		if (in_array ( $_REQUEST ['sfield'], array (
				'username' 
		) ))
			$main_table = 'u';
		if ($_REQUEST ['sfield'] == 'id')
			$key = "{$main_table}.id={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "{$main_table}.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		$sql_s = "select a.id,a.title,a.info,a.province,a.city,a.status,a.district,a.create_time,s.name,s.user_id,s.id siteid
			from article as a
			left join site as s on s.user_id=a.user_id where {$key} ORDER BY a.{$order}";
		$sql_c = "select count(*) num 
				from article as a left join site as s on s.user_id=a.user_id where {$key}";
		$sql_f = 'num';
		
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		foreach ( $list as &$value ) {
			$shops = M ()->query ( "select group_concat(name SEPARATOR '') address from `area` where id in({$value['province']},{$value['city']},{$value['district']})" );
			$value ['address'] = $shops [0] ['address'];
		}
		$this->assign ( 'list', $list ); // dump($list);
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 客户列表
	 */
	function consumer_list() {
		// $condition = parent::create_where ();
		$key = '1';
		$type = $_REQUEST ['method'];
		$order = $this->orderField . ' ' . $this->orderDirection;
		if ($_REQUEST ['sfield'] == 'id')
			$key = "u.id={$_REQUEST['keywords']}";
		elseif ($_REQUEST ['sfield'] && $_REQUEST ['sfield'] != 'all')
			$key = "u.{$_REQUEST['sfield']} like '%{$_REQUEST['keywords']}%'";
		if ($type == 'order') {
			$sql_s = "select s.name as sname,u.id,u.username,u.email,u.mobile,u.createtime
				FROM order_ext as oe 
				LEFT JOIN order_ext oee ON oe.order_id=oee.order_id 
				LEFT JOIN `user` u ON oe.`value`=u.id 
				LEFT JOIN `site` s ON oe.`value`=s.user_id 
				WHERE oe.element='userid' AND oee.element='cid' and {$key} ORDER BY u.{$order}";
			$sql_c = "select count(*) as num
				FROM order_ext as oe 
				LEFT JOIN order_ext oee ON oe.order_id=oee.order_id 
				LEFT JOIN `user` u ON oe.`value`=u.id 
				LEFT JOIN `site` s ON oe.`value`=s.user_id 
				WHERE oe.element='userid' AND oee.element='cid' and {$key}";
		} else {
			$sql_s = "select s.name as sname,u.id,u.username,u.email,u.mobile,u.createtime
				from user_ext as ue
				left join `user` as u on u.id=ue.userid
				left join site as s on s.user_id=ue.value
				where ue.element='recom' and {$key} ORDER BY u.{$order}";
			$sql_c = "select count(*) as num
				from user_ext as ue
				left join `user` as u on u.id=ue.userid
				left join site as s on s.user_id=ue.value
				where ue.element='recom' and {$key}";
		}
		$sql_f = 'num';
		
		$list = self::_select ( M (), array (
				$sql_s,
				$sql_c,
				$sql_f 
		) );
		$this->assign ( 'list', $list );
		$this->assign ( 'method', $type ); // dump($list);
		$this->assign ( 'sfield', $_REQUEST ['sfield'] );
		$this->display ();
	}
	
	/**
	 * 流量列表
	 */
	function flow_product() {
		$this->display ();
	}
	
	/**
	 * 流量列表
	 */
	function ajax_doen() {
		// $a = var_export($_REQUEST,true);
		$id = $_REQUEST ['id'];
		$mod = $_REQUEST ['mod'];
		$type = $_REQUEST ['type'];
		$status = $_REQUEST ['status'];
		$fstatus = $_REQUEST ['fstatus'] ? $_REQUEST ['fstatus'] : 'status';
		if ($type == 'update') {
			$m = M ( $mod );
			$re = $m->save ( array (
					'id' => $id,
					$fstatus => $status 
			) );
			echo '--' . $m->getLastSql ();
		}
		// $this->display();
	}
	
	/**
	 * 通用查询
	 * 
	 * @param object $dao
	 *        	操作列表页面对象
	 * @param array $sql
	 *        	查询语句，array(列表语句,总列表条数,总列表条数语句的关键字)
	 * @param array $where
	 *        	查询条件
	 * @return 查询列表
	 */
	function _select($dao, $sql = array(), $where = array()) {
		$condition = parent::create_where ();
		$condition = array_merge ( $condition, $where );
		$order = $this->orderField . ' ' . $this->orderDirection;
		
		if ($sql [1]) {
			$count = M ()->query ( $sql [1] );
			$count = $count [0] [$sql [2]];
		} else
			$count = $dao->where ( $condition )->count ();
		
		if ($count > 0) {
			$this->assign ( "totalCount", $count );
			$page = new Page ( $count, $this->listrows );
			if ($sql [0]) {
				$sql [0] .= " limit {$page->firstRow},{$page->listRows} ";
				$list = $dao->query ( $sql [0] );
			} else
				$list = $dao->where ( $where )->order ( $order )->select ();
		}
		return $list;
	}
	
	/**
	 * 更新
	 */
	function _update() {
	}
	
	/**
	 * 添加
	 */
	function _add() {
	}
	
	/**
	 * 删除
	 */
	function _delete() {
	}
	
	// 访问统计量(月)
	function countf() {
		$countr = self::showclick ();
		$countex = self::showex ();
		$arr = array (
				$countr,
				$countex 
		);
		echo json_encode ( $arr );
	}
	// 点击明细每月
	function countfd() {
		$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from clicks a1,product_description a2 where from_unixtime(a1.intime,'%m') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	// 曝光明细每月
	function countfb() {
		$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from exposure a1,product_description a2 where from_unixtime(a1.intime,'%m') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	
	// 访问统计量(时)
	function counthour() {
		// 访问产品统计
		$countr = self::hourclick ();
		$countex = self::hourex ();
		$arr = array (
				$countr,
				$countex 
		);
		echo json_encode ( $arr );
	}
	// 点击明细小时
	function counthourd() {
		$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from clicks a1,product_description a2 where from_unixtime(a1.intime,'%H') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	// 曝光明细小时
	function counthourb() {
		$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from exposure a1,product_description a2 where from_unixtime(a1.intime,'%H') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	
	// 访问统计量(日)
	function countday() {
		// 访问产品统计
		$countr = self::dayclick ();
		$countex = self::dayex ();
		$arr = array (
				$countr,
				$countex 
		);
		echo json_encode ( $arr );
	}
	// 点击明细每日
	function countdayd() {
		$start = strtotime ( date ( 'Y-M' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from clicks a1,product_description a2 where from_unixtime(a1.intime,'%d') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	// 曝光明细每日
	function countdayb() {
		$start = strtotime ( date ( 'Y-M' ) );
		$sql = "select a1.address,from_unixtime(a1.intime,'%Y-%m-%d %H:%i:%s') time,a2.name,a1.referrer from exposure a1,product_description a2 where from_unixtime(a1.intime,'%d') = " . $_REQUEST ["date"] . " and a1.user_id = " . $_SESSION ["userid"] . " and a2.product_id = a1.productid and intime BETWEEN " . $start . " and " . time () . " and a1.plantform='biz' ORDER BY time desc";
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	
	/**
	 * 商品点击量查询(月)
	 */
	function showclick() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
		// $sql = "select from_unixtime(intime,'%m') as date,count(id) count,MONTH(CURdate()) m from clicks where user_id = ".$_SESSION["userid"]." and intime BETWEEN ".$start." and ".time()." and plantform='biz' group by date desc LIMIT 0,12";
		$sql = "select from_unixtime(intime,'%m') as date,count(id) count,MONTH(CURdate()) m from clicks where intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date desc LIMIT 0,12";
		$blag = M ()->query ( $sql );
		$re = array ();
		
		return $blag;
	}
	
	/**
	 * 商品点击量查询(当天每小时)
	 */
	function hourclick() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
		// $sql = "select from_unixtime(intime,'%H') as date,count(id) count,hour(now()) m from clicks where user_id = ".$_SESSION["userid"]." and intime BETWEEN ".$start." and ".time()." and plantform='biz' group by date desc";
		$sql = "select from_unixtime(intime,'%H') as date,count(id) count,hour(now()) m from clicks where intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date desc";
		$blag = M ()->query ( $sql );
		return $blag;
	}
	
	/**
	 * 商品点击量查询(当月每日)
	 */
	function dayclick() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-M' ) );
		$sql = "select from_unixtime(intime,'%e') as date,count(id) count,day(CURdate()) m from clicks where user_id = " . $_SESSION ["userid"] . " and intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date order by date desc";
		// $sql = "SELECT * FROM `clicks` WHERE user_id = ".$_SESSION["userid"];
		$blag = M ()->query ( $sql );
		return $blag;
	}
	
	/**
	 * 商品曝光量查询(月)
	 */
	function showex() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
		// $sql = "select from_unixtime(intime,'%m') as date,count(id) count,MONTH(CURdate()) m from exposure where user_id = ".$_SESSION["userid"]." and intime BETWEEN ".$start." and ".time()." and plantform='biz' group by date desc LIMIT 0,12";
		$sql = "select from_unixtime(intime,'%m') as date,count(id) count,MONTH(CURdate()) m from exposure where user_id = " . $_SESSION ["userid"] . " and intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date desc LIMIT 0,12";
		// $sql = "SELECT * FROM `clicks` WHERE user_id = ".$_SESSION["userid"];
		$blag = M ()->query ( $sql );
		return $blag;
	}
	
	/**
	 * 商品曝光量查询(当天每小时)
	 */
	function hourex() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
		// $sql = "select from_unixtime(intime,'%H') as date,count(id) count,hour(now()) m from exposure where user_id = ".$_SESSION["userid"]." and intime BETWEEN ".$start." and ".time()." and plantform='biz' group by date desc";
		$sql = "select from_unixtime(intime,'%H') as date,count(id) count,hour(now()) m from exposure where  intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date desc";
		// $sql = "SELECT * FROM `clicks` WHERE user_id = ".$_SESSION["userid"];
		$blag = M ()->query ( $sql );
		return $blag;
	}
	
	/**
	 * 商品曝光量查询(当月每日)
	 */
	function dayex() {
		$outtime = gettimeofday ();
		$start = strtotime ( date ( 'Y-M' ) );
		$sql = "select from_unixtime(intime,'%e') as date,count(id) count,day(CURdate()) m from exposure where user_id = " . $_SESSION ["userid"] . " and intime BETWEEN " . $start . " and " . time () . " and plantform='biz' group by date desc";
		// $sql = "SELECT * FROM `clicks` WHERE user_id = ".$_SESSION["userid"];
		$blag = M ()->query ( $sql );
		return $blag;
	}
	
	/**
	 * 饼状图
	 */
	function pie() {
		$type = $_REQUEST ["type"];
		$datetype = $_REQUEST ["datetype"];
		$date = $_REQUEST ["date"];
		$sql = "";
		if ($type == "d") {
			if ($datetype == "month") {
				$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
				$sql = "select a2.name,count(a1.id) count from clicks a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%m') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			} else if ($datetype == "day") {
				$start = strtotime ( date ( 'Y-M' ) );
				$sql = "select a2.name,count(a1.id) count from clicks a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%d') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			} else {
				$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
				$sql = "select a2.name,count(a1.id) count from clicks a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%H') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			}
		} else {
			if ($datetype == "month") {
				$start = strtotime ( date ( 'Y-01-01 00:00:00' ) );
				$sql = "select a2.name,count(a1.id) count from exposure a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%m') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			} else if ($datetype == "day") {
				$start = strtotime ( date ( 'Y-M' ) );
				$sql = "select a2.name,count(a1.id) count from exposure a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%d') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			} else {
				$start = strtotime ( date ( 'Y-M-d 00:00:00' ) );
				$sql = "select a2.name,count(a1.id) count from exposure a1,product_description a2 where a1.productid = a2.product_id and from_unixtime(a1.intime,'%H') = " . $date . " and a1.intime BETWEEN " . $start . " and " . time () . " and a1.user_id = " . $_SESSION ["userid"] . " and a1.plantform='biz' GROUP BY a2.name order by count desc";
			}
		}
		$arr = M ()->query ( $sql );
		echo json_encode ( $arr );
	}
	/**
	 * ************广 告************
	 */
	function add_ads() {
		// echo json_encode($_REQUEST);;die();
		// 广告信息新增或修改
		$id = $_REQUEST ['id'];
		$user_id = $_REQUEST ['user_id'];
		$mod = $_REQUEST ['mod'];
		if ($id != null) {
			// 获取广告信息
			$ads = M ()->query ( "SELECT a.*,l.id layoutid,l.layout FROM  ads a left join layout l on a.id=l.cell_id  WHERE a.id = {$id}" );
		}
		$this->assign ( 'ads', $ads [0] );
		$this->assign ( 'id', $id );
		$this->assign ( 'user_id', $user_id );
		// $this->assign('mod', $mod);
		$this->display ();
	}
	function ads_updateoradd() {
		$adsdata = array ();
		$filePath = $this->_upload ();
		$adsdata ['script'] = $filePath;
		if (! $adsdata ['script'])
			unset ( $adsdata ['script'] );
		foreach ( $_REQUEST as $key => $value ) {
			$adsdata [$key] = $value;
		}
		$reid = M ( 'ads' )->add ( $adsdata, array (), true );
		
		if ($reid > 0) {
			$temp = array ();
			$temp ['id'] = $_REQUEST ['layoutid'];
			$temp ['layout'] = $_REQUEST ['layout'];
			$temp ['user_id'] = $_REQUEST ['user_id'];
			$temp ['cell_id'] = $reid;
			$temp ['layout'] = $_REQUEST ['layout'];
			$temp ['type'] = 3;
			$temp ['status'] = 1;
			M ( 'layout' )->add ( $temp, array (), true );
		}
		$this->_ajm ( 200, '数据操作成功', 0, $url ? $url : U ( MODULE_NAME . '/ad_list' ) );
		$this->display ();
	}
	/**
	 * ************广 告************
	 */
	
	/**
	 * ************文 章************
	 */
	function article_add() {
		// echo json_encode($_REQUEST);;die();
		// 广告信息新增或修改
		$id = $_REQUEST ['id'];
		$user_id = $_REQUEST ['user_id'];
		$siteid = $_REQUEST ['siteid'];
		$mod = $_REQUEST ['mod'];
		if ($id != null) {
			// 获取广告信息
			$article = M ()->query ( "select a.*,s.id siteid,s.name,s.user_id
			from article as a
			left join site as s on s.user_id=a.user_id
			where a.id={$id}" );
		}
		$this->assign ( 'article', $article [0] );
		$this->assign ( 'id', $id );
		$this->assign ( 'user_id', $user_id );
		$this->assign ( 'siteid', $siteid );
		$this->display ();
	}
	function article_updateoradd() {
		$data = array ();
		foreach ( $_REQUEST as $key => $value ) {
			$data [$key] = $value;
		}
		M ( 'article' )->add ( $data, array (), true );
		$this->_ajm ( 200, '数据操作成功', 0, $url ? $url : U ( MODULE_NAME . '/article_list' ) );
		$this->display ();
	}
	/**
	 * ************文 章************
	 */
	
	/**
	 * 更新所有商品的价格缓存信息
	 * @date 2014-5-2
	 * @time 下午4:27:40
	 */
	function updatecache() {
		// 更新所有商品的价格缓存信息
		$p = $_REQUEST [C ( 'VAR_PAGE' )];
		$o2o = M ( 'ProductO2o' );
		$count = $o2o->count ('id');
// 		echo($o2o->getLastSql());
		$page = new Page ( $count, 100 );
		$data = $o2o->field ( 'user_id,product_id,status' )->limit ( $page->firstRow . ',100' )->select ();
// 		echo('<br>');
// 		echo($o2o->getLastSql());
		$i = 0;
		foreach ( $data as $v ) {
			$i ++;
			$res = self::_pro_bascinfo ( $v ['user_id'], $v ['product_id'],$v['status'] );
			echo $i . '  写入成功<br>';
		}
		$np = $p + 1;
		echo '<script>setTimeout(window.location.href="' . __URL__ . '-updatecache-' . C ( 'VAR_PAGE' ) . '-' . $np . '",800000);</script>';
		echo '<a href="' . __URL__ . '-updatecache-' . updatecache . '-' . $np . '">下一页</a>';
	} // end updatecache
	
	/**
	 * 获取基本信息
	 * 
	 * @param int $_uid
	 *        	商户id
	 * @param int $pid
	 *        	产品id
	 * @return array $data
	 */
	protected function _pro_bascinfo($_uid, $pid,$status) {
		$sql="SELECT p.code,p.image,p.ygstatus,p.name,p.mian_cat cate_id,p.price,ppo.price,ppo.add,ppo.percent FROM `product_price_o2o` as ppo LEFT JOIN product as p on ppo.product_id = p.id ";
		$sql.="WHERE ( ppo.user_id = '".$_uid."' ) AND ( ppo.product_id = '".$pid."' ) LIMIT 1";
		$res=M()->query($sql);
// 		echo('<br>');
// 		echo(M()->getLastSql());
		$res=$res[0];
		$data ['product_id'] = $pid;
		$data ['user_id'] = $_uid;
		$data ['jiayi'] = $res ['jiayi'];
		// 计算价格
		$sale = 0;
		if ($res ['price'] > 0) {
			$sale = $res ['price'];
		} elseif ($res ['add'] > 0) {
			$sale = intval ( $res ['jiayi'] ) + intval ( $res ['add'] );
		} elseif ($res ['percent'] > 0) {
			$sale = round ( intval ( $res ['jiayi'] ) * (1 + intval ( $res ['percent'] ) * 0.01) );
		}
		$data ['sale'] = $sale;
		$data ['name'] = $res ['name'];
		$data ['code'] = $res ['code'];
		$data ['image'] = $res ['image'];
		$data ['status'] = empty ($status) ? 1 :$status;
		$data ['ygstatus'] = $res ['ygstatus'];
		
		// 获取产品分类
		$data ['cate_id'] = $res['cate_id'];
		$where = array ();
		$where ['product_id'] = $pid;
		$where ['user_id'] = $_uid;
		$res = M ( 'ProductDesO2o' )->where ( $where )->save ( $data );
// 		echo('<br>');
// 		echo(M('ProductDesO2o')->getLastSql());
// 		exit;
		return $res;
	}
}//end class









